/**
 * Created with IntelliJ IDEA.
 * User: Ruslan Konovalov
 * Date: 18.01.14
 * Time: 1:14
 * To change this template use File | Settings | File Templates.
 */

var REGISTERED_MODULES =
    [
        {name: "ReservationClass", loaded: false, path: ""},
        {name: "AdminClass", loaded: false, path: ".\\js\\admin\\"}
    ];

const SCRIPT_PATH = ".\\js\\";
const PACKAGERELOAD_INTERVAL = 500;
var ModulesLoaded = false;



function LoadFile(filename, filetype){
    if (filetype=="js"){ //if filename is an external JavaScript file
        var fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        var fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}

function StartLoadModules()
{
    for(var index in REGISTERED_MODULES)
    {
        if(REGISTERED_MODULES[index].path != "")
        {
            path = REGISTERED_MODULES[index].path;
        } else
            path = SCRIPT_PATH;
        LoadFile(path + REGISTERED_MODULES[index].name + ".js", "js");
    }
}

/*This function checck for loaded states of modules registered in REGISTERED_MODULES variable*/
function CheckModulesLoaded()
{
    for(var index in REGISTERED_MODULES)
    {
        if(typeof(window[REGISTERED_MODULES[index].name]) === "undefined")
        {
            OnModuleNotLoaded(REGISTERED_MODULES[index].name);
            ModulesLoaded = false;

            setTimeout(CheckModulesLoaded, PACKAGERELOAD_INTERVAL);
            return false;
        } else
        {
            REGISTERED_MODULES[index].loaded = true;
        }
    }
    ModulesLoaded = true;
    Run();
    return true;
}

function ModuleLoaded(moduleName)
{
    for(var index in REGISTERED_MODULES)
    {
        if(REGISTERED_MODULES[index].name.toUpperCase() == moduleName.toUpperCase())
        {
          return REGISTERED_MODULES[index].loaded;
        }
    }
    return false;
}

setTimeout(CheckModulesLoaded, PACKAGERELOAD_INTERVAL);
StartLoadModules();

function OnModuleNotLoaded(moduleName)
{
    alert(moduleName);
}


function Run()
{
    alert(ReservationClass.getInfo());
}